package com.zb.mobile.user.controller;

import com.zb.annotation.SystemLog;
import com.zb.constant.LogConstant;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.SelectVo;
import com.zb.exception.SMSException;
import com.zb.exception.VendorIdentifierException;
import com.zb.mobile.UserOrderExpectedTimeVo;
import com.zb.mobile.UserOrderSelectVo;
import com.zb.mobile.UserOrderVo;
import com.zb.mobile.user.service.ConUserMobileOrderService;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjWorkModuleFieldService;
import com.zb.utils.TokenUserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @Author WEN
 * @Date 2023/11/2 0002
 **/
@Api(tags = "用户报单移动端工单类接口")
@RestController
@RequestMapping("/mobile/user/order")
public class UserOrderController {
    @Resource
    private ConUserMobileOrderService conUserMobileOrderService;
    @Resource
    private ZjWorkModuleFieldService workModuleFieldService;

    @ApiOperation("服务进度-获取工单状态数量")
    @PostMapping("/getOrderStatusQty")
    public Result getOrderStatusQty(HttpServletRequest request, @RequestBody UserOrderSelectVo selectVo) throws SMSException, VendorIdentifierException {
        AccounLoginInfo loginInfo = TokenUserUtil.getAccountLoginInfo(request);
        if (null == loginInfo) {
            loginInfo = new AccounLoginInfo(0L, "", "", "", "");
            loginInfo.setCustAccId(TokenUserUtil.getCustAccIdByMobildUserCustCode(request));
        }
        return Result.success().data(conUserMobileOrderService.getOrderStatusQty(loginInfo.getCustAccId(), loginInfo.getId(), selectVo));
    }

    @ApiOperation("服务进度-获取工单列表")
    @PostMapping("/getOrderList")
    public Result getOrderList(HttpServletRequest request, @RequestBody UserOrderSelectVo selectVo) throws SMSException, VendorIdentifierException {
        AccounLoginInfo loginInfo = TokenUserUtil.getAccountLoginInfo(request);
        if (null == loginInfo) {
            loginInfo = new AccounLoginInfo(0L, "", "", "", "");
            loginInfo.setCustAccId(TokenUserUtil.getCustAccIdByMobildUserCustCode(request));
        }
        return Result.success().data(conUserMobileOrderService.getOrderList(loginInfo.getCustAccId(), loginInfo.getId(), selectVo));
    }

    @ApiOperation("服务进度-根据工单ID获取工单列表详情")
    @GetMapping("/getOrderDetail/{workId}")
    public Result getOrderDetail(@PathVariable @ApiParam(value = "工单ID", required = true) Long workId) {
        return Result.success().data(PageUtil.row(conUserMobileOrderService.getOrderDetail(workId)));
    }

    @ApiOperation("服务进度-获取工单列表详情的服务进度")
    @PostMapping("/getOrderDetailServiceLog/{workId}")
    public Result getOrderDetailServiceLog(@PathVariable @ApiParam(value = "工单ID", required = true) Long workId, @RequestBody PageUtil pageUtil) {
        return Result.success().data(conUserMobileOrderService.getOrderServiceLog(workId, pageUtil));
    }

    @ApiOperation("服务进度-获取工单列表详情的商品信息")
    @GetMapping("/getOrderDetailGoodsInfo/{workId}")
    public Result getOrderDetailGoodsInfo(@PathVariable @ApiParam(value = "工单ID", required = true) Long workId) {
        return Result.success().data(PageUtil.row(conUserMobileOrderService.getOrderDetailGoodsInfo(workId)));
    }

    @ApiOperation("服务进度-获取工单列表详情的工单信息")
    @GetMapping("/getOrderDetailOrderInfo/{workId}")
    public Result getOrderDetailOrderInfo(@PathVariable @ApiParam(value = "工单ID", required = true) Long workId) {
        return Result.success().data(PageUtil.row(conUserMobileOrderService.getOrderDetailOrderInfo(workId)));
    }

    @ApiOperation("服务进度-获取工单列表详情的服务商信息")
    @GetMapping("/getOrderDetailNetworkInfo/{workId}")
    public Result getOrderDetailNetworkInfo(@PathVariable @ApiParam(value = "工单ID", required = true) Long workId) {
        return Result.success().data(PageUtil.row(conUserMobileOrderService.getOrderDetailNetworkInfo(workId)));
    }

    @ApiOperation("服务进度-获取工单列表详情的客户信息")
    @GetMapping("/getOrderDetailUserInfo/{workId}")
    public Result getOrderDetailUserInfo(@PathVariable @ApiParam(value = "工单ID", required = true) Long workId) {
        return Result.success().data(PageUtil.row(conUserMobileOrderService.getOrderDetailUserInfo(workId)));
    }

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, empType = LogConstant.SYSTEM_LOG_EMP_TYPE_2, remark = "服务进度-取消服务工单")
    @ApiOperation("服务进度-取消服务工单")
    @PutMapping("/cancelOrder/{workId}")
    public Result cancelOrder(HttpServletRequest request, @PathVariable @ApiParam(value = "工单ID", required = true) Long workId) {
        AccounLoginInfo loginInfo = TokenUserUtil.getAccountLoginInfo(request);
        conUserMobileOrderService.cancelOrder(loginInfo, workId);
        return Result.success("取消服务工单成功");
    }

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, empType = LogConstant.SYSTEM_LOG_EMP_TYPE_2, remark = "服务进度-改约日期时间")
    @ApiOperation("服务进度-改约日期时间")
    @PutMapping("/updateExpectedTime")
    public Result updateExpectedTime(HttpServletRequest request, @RequestBody @Valid UserOrderExpectedTimeVo orderExpectedTimeVo) {
        AccounLoginInfo loginInfo = TokenUserUtil.getAccountLoginInfo(request);
        conUserMobileOrderService.updateExpectedTime(loginInfo, orderExpectedTimeVo);
        return Result.success("更改期望上门日期时间成功");
    }

    @ApiOperation("历史工单报单-历史工单列表")
    @PostMapping("/getUserHistoryOrder")
    public Result getUserHistoryOrder(HttpServletRequest request, @RequestBody SelectVo selectVo) throws VendorIdentifierException {
        AccounLoginInfo loginInfo = TokenUserUtil.getAccountLoginInfo(request);
        if (null == loginInfo) {
            loginInfo = new AccounLoginInfo(0L, "", "", "", "");
            loginInfo.setCustAccId(TokenUserUtil.getCustAccIdByMobildUserCustCode(request));
        }
        return Result.success().data(conUserMobileOrderService.getUserHistoryOrder(loginInfo, selectVo));
    }
    @ApiOperation("历史工单报单-历史工单列表详情")
    @GetMapping("/getUserHistoryOrderDetail/{workId}")
    public Result getUserHistoryOrderDetail(@PathVariable @ApiParam(value = "工单ID", required = true) Long workId) {
        return Result.success().data(PageUtil.row(conUserMobileOrderService.getUserHistoryOrderDetail(workId)));
    }

    @ApiOperation("历史工单报单-历史工单商品信息")
    @PostMapping("/getUserHistoryOrdeGoods/{workId}")
    public Result getUserHistoryOrdeGoods(@PathVariable @ApiParam(value = "工单ID") Long workId, @RequestBody PageUtil pageUtil) {
        return Result.success().data(conUserMobileOrderService.getUserHistoryOrdeGoods(workId, pageUtil));
    }

    @ApiOperation("历史工单报单-历史工单商品编码信息")
    @GetMapping("/getUserHistoryOorderGoodsCodes/{workGoodsId}")
    public Result getUserHistoryOorderGoodsCodes(@PathVariable @ApiParam(value = "工单商品ID") Long workGoodsId) {
        return Result.success().data(PageUtil.row(conUserMobileOrderService.getUserHistoryOorderGoodsCodes(workGoodsId)));
    }

    @ApiOperation("历史工单报单-获取历史工单申请售后的商品")
    @GetMapping("/userHistoryOrderApplySale/{workId}")
    public Result gettUerHistoryOrderApplySale(@PathVariable @ApiParam(value = "工单ID") Long workId) {
        return Result.success().data(PageUtil.row(conUserMobileOrderService.gettUerHistoryOrderApplySale(workId)));
    }

    @ApiOperation("获取工单报单的自定义字段")
    @GetMapping("/getUserOrderModuleField/{moduleId}")
    public Result getUserOrderModuleField(@PathVariable @ApiParam(value = "工单模块ID", required = true) Long moduleId) {
        return Result.success().data(PageUtil.rows(workModuleFieldService.getModuleField(false, moduleId)));
    }

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, empType = LogConstant.SYSTEM_LOG_EMP_TYPE_2, remark = "客户移动端报单")
    @ApiOperation("创建工单")
    @PostMapping("/saveUserOrder")
    public Result saveUserOrder(HttpServletRequest request, @RequestBody @Valid UserOrderVo userOrderVo) throws VendorIdentifierException {
        AccounLoginInfo loginInfo = TokenUserUtil.getAccountLoginInfo(request);
        if (null == loginInfo) {
            Long custAccId = TokenUserUtil.getCustAccIdByMobildUserCustCode(request);
            loginInfo = new AccounLoginInfo(0L, "", "", "", "");
            loginInfo.setId(0L);
            loginInfo.setCustAccId(custAccId);
        }
        return Result.success("提交工单成功").data(conUserMobileOrderService.saveUserOrder(loginInfo, userOrderVo));
    }
}
